import java.io.*;
import java.util.*;

public class Main {
	BufferedReader bf;
	StringTokenizer st;
	StringBuilder sb;

	int c,
		n,
		k;
	
	public static void main(String[] args) {
		Main main = new Main();
		main.beginProcess();
	}
	
	public Main() {
		bf = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
	}

	public void beginProcess() {
		try {
			st = new StringTokenizer(bf.readLine());
			c = Integer.parseInt(st.nextToken());
			
			while(c != 0) {
				st = new StringTokenizer(bf.readLine());
				n = Integer.parseInt(st.nextToken());
				st = new StringTokenizer(bf.readLine());
				k = Integer.parseInt(st.nextToken());
				
				theLastOne();
				
				c--;
			}
			
			System.out.print(sb);
		}
		catch(Exception ex) {
			System.err.println(ex.toString());
		}
	}
	
	private void theLastOne() {
		int[] student = new int[n];
		int left = 0,
			i,
			pos = 0;
		
		for(i = 0; i < student.length; i++)
			student[i] = 0;
		
		while(left < n - 1) {
			i = 0;
			while(i < k) {
				if(student[pos] == 0)
					i++;
				
				pos++;
				if(pos == n)
					pos = 0;
			}
			
			if(pos == 0) {
				student[n - 1] = 1;
			} else {
				student[pos - 1] = 1;
			}
			
			left++;
		}
		
		for(i = 0; i < student.length; i++)
			if(student[i] == 0)
				sb.append((i + 1) + "\n");
	}
	
}